package Util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.commons.dbcp.BasicDataSource;

public class DBUtil {
	private static final String CLASSPATH = "oracle.jdbc.OracleDriver";
	private static final String URL = "jdbc:oracle:thin:@10.222.29.165:1521:xe";
	private static final String USERNAME = "matthew";
	private static final String PASSWORD = "123456";
	
	private static BasicDataSource ds = null;
	static {
		ds = new BasicDataSource();
		ds.setUrl(URL);
		ds.setUsername(USERNAME);
		ds.setPassword(PASSWORD);
		ds.setDriverClassName("oracle.jdbc.OracleDriver");
		ds.setMaxActive(150);
		ds.setInitialSize(30);
		
	}
	
//	public static Connection getConnection(){
//		Connection con = null;
//		try {
//			Class.forName(CLASSPATH);
//			con = DriverManager.getConnection(URL, USERNAME, PASSWORD);
//		} catch (ClassNotFoundException e) {
//			e.printStackTrace();
//		} catch (SQLException e) {
//			e.printStackTrace();
//		}
//		return con;
//	}
	
	public static Connection getConnection(){
		Connection con = null;
		try {
			 con =  ds.getConnection();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return con;
	}
	
	public static void close(Connection con,PreparedStatement pst, ResultSet rs){
		try {
			if(rs != null){
				rs.close();
			}
			if(pst != null){
				pst.close();
			}
			if(con != null){
				con.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
	}
}
